// map.js
import qqmap from '../../../utils/qqmap'
Page({
  data: {
    latitude: '',
    longitude: '',
    locationName: '潮阳力嘉中学',
    repairInfo: ''
  },
  onLoad({ id }) {
    this.getRepairInfo(id)
    this.geocoder(this.data.locationName)
  },
  async getRepairInfo(id) {
    const res = await wx.http({
      url: `/repair/${id}`
    })
    console.log(res);
    this.setData({
      repairInfo: res.data
    })
  },
  geocoder(address) {
    qqmap.geocoder({
      address,
      success: (res) => {
        const latitude = res.result.location.lat
        const longitude = res.result.location.lng
        this.setData({
          latitude, longitude
        })
      }
    })
  },
  onGoUpdateRepair() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.repairInfo.id
    })
  },
  onCancelRepair() {
    wx.showModal({
      title: '提示',
      content: '你确定要取消吗',
      success: async ({ confirm }) => {
        console.log(confirm);
        const res = await wx.http({ url: `/cancel/repaire/${this.data.repairInfo.id}`, method: 'put' })
        if (res.code !== 10000) {
          return wx.utils.toast(res.message)
        }
        wx.showToast({
          title: '取消成功',
        })
        setTimeout(() => {
          wx.navigateTo({ url: '/repair_pkg/pages/list/index' })
        }, 1500)
      }
    })
  }
})
